package com.sxm.yiwei.tools;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.sxm.yiwei.R;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by KuKa on 2017/10/16.
 * 年/月/日时间弹窗( 申请爱心店铺)
 */

public class TimeWindowS extends PopupWindow{
    TimeOnClick timeOnClick;
    int yaer;
    int month;
    int day;
    String time = null;
    public TimeWindowS(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.timewindow, null);
        TextView sure = (TextView) view.findViewById(R.id.timepop_sure);
        TextView cancel = (TextView) view.findViewById(R.id.timepop_cancle);
        final DatePicker Data = (DatePicker) view.findViewById(R.id.timepop_data);
        // 设置视图
        this.setContentView(view);

        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);

        long l = System.currentTimeMillis();
        Date date1 = new Date(l);// 订单时间
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        final int yaer1 = calendar1.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
        yaer = yaer1;
        month = month1+1;
        day = day1;
        Data.init(yaer1, month1, day1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                yaer = year;
                month = monthOfYear+1;
                day = dayOfMonth;
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timeOnClick!=null){
                    timeOnClick.onClickCancel();
                }
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time = yaer + "-" + month + "-" + day;
                if (timeOnClick!=null){
                    timeOnClick.onClickSure(time);
                }
            }
        });
    }

    public interface TimeOnClick{
         void onClickCancel();
         void onClickSure(String time);
    }
    public void setTimeOnClick(TimeOnClick timeOnClick){
        this.timeOnClick = timeOnClick;
    }
}
